package com.gemme.gemme.setting;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.gemme.gemme.BaseActivity;
import com.gemme.gemme.MainActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.login.Constant;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.manager.DataPares;
import com.gemme.gemme.util.SPUtil;
import com.gemme.gemme.view.GeneralDialog;
import com.gemme.gemme.view.Topview;
import com.gemme.gemme.view.TvTvArrow;
import com.gemme.gemme.wode.WoAccountActivity;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.ChatClient;
import com.hyphenate.chat.EMClient;
import com.hyphenate.helpdesk.callback.Callback;
import com.jaeger.library.StatusBarUtil;

import java.util.HashMap;

public class Setting2Activity extends BaseActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting2);
        initView();
        initTop();
    }

    private void initTop() {
//        StatusBarUtil.setColor(this,getResources().getColor(R.color.white_color) );
        StatusBarUtil.setTransparent(this);
//        StatusBarUtil.setTranslucent(this,250);
        Topview topview = findViewById(R.id.topview);
        topview.setContentText("设置");
    }
    TvTvArrow  tta_wdzh;
    TvTvArrow  tta_yjfk;
    TvTvArrow  tta_yhxy;
    TextView tv_loginOut;
    private void initView() {
        tta_wdzh =  findViewById(R.id.tta_wdzh);
        tta_yjfk =  findViewById(R.id.tta_yjfk);
        tta_yhxy =  findViewById(R.id.tta_yhxy);


        tv_loginOut = findViewById(R.id.tv_loginOut);

        tta_wdzh.setLeftText("我的账号").setArrowVisble(true);
        tta_yjfk.setLeftText("意见反馈").setArrowVisble(true);
        tta_yhxy.setLeftText("用户协议").setArrowVisble(true);

        tta_wdzh.setOnClickListener(this);
        tta_yjfk.setOnClickListener(this);
        tta_yhxy.setOnClickListener(this);
        tv_loginOut.setOnClickListener(this);

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tta_wdzh:
                startActivity(new Intent(mContext, WoAccountActivity.class));
                break;

            case R.id.tta_yjfk:
                startActivity(new Intent(mContext,AdviceActivity.class));
                break;

            case R.id.tta_yhxy:
                startActivity(new Intent(mContext,XieyiActivity.class));
                break;

            case R.id.tv_loginOut:

                GeneralDialog dialog = new GeneralDialog();
                dialog.showDialog(mContext);
                dialog.setContent("确认退出吗?");

                dialog.getTv_confirm().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        token = DataManager.getToken(mContext);
                        cleardata();
                        String refresh_token = SPUtil.getDataForString(mContext, Constant.REFRESH_TOKEN, "");

                        new Thread(new Runnable() {
                            @Override
                            public void run() {
//                        DataManager.refreshToken(mContext);
                                loginOut();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        cleardata();
//                                        Intent intent_login = new Intent(mContext, LoginNewActivity.class);
//                                        ActivityCollectorUtil.finishAllActivity();
//                                        startActivity(intent_login);

                                        finish();
                                        setResult(Activity.RESULT_OK, null);
                                    }
                                });
                            }
                        }).start();

                        hxLogout();

                        hxkfLoginOut();

                        dialog.dismisss();
                    }
                });






                break;



        }
    }


    String token;

    private void loginOut() {


        //http://192.168.1.6:9999/auth/token/applogout
        String url = DataManager.BaseUrl + "auth/token/applogout";
        HashMap<String, String> headHashMap = new HashMap<>();
        SharedPreferences MSP = getSharedPreferences(Constant.MSP, Context.MODE_PRIVATE);

//        headHashMap.put(DataManager.AuthorizationKey,DataManager.getToken(mContext));
        headHashMap.put(DataManager.AuthorizationKey,token);
        DataManager.toServer2(mContext, url, DataManager.DELETE_TYPE, "", headHashMap, new DataManager.GeResponse(){

            @Override
            public void onResponse(String response) {

                try {
                    System.out.println("");
//                    int code = (new JSONObject(response)).getInt("code");
                    if(DataPares.isSucess(mContext,response)){
                        Toast.makeText(mContext, "退出登录成功", Toast.LENGTH_SHORT).show();
                        cleardata();
                        startActivity(new Intent(mContext, MainActivity.class));
//                        startActivity(new Intent(mContext, LoginNewActivity.class));
                    }else{
                        Toast.makeText(mContext, "退出登录失败", Toast.LENGTH_SHORT).show();
//                        Log.d();
                    }

                    finish();
                }catch (Exception e){

                }
            }
        });
    }

    //环信退出登录
    void hxLogout() {
        final ProgressDialog pd = new ProgressDialog(mContext);
        String st = getResources().getString(R.string.Are_logged_out);
        pd.setMessage(st);
        pd.setCanceledOnTouchOutside(false);
        pd.show();
        EMClient.getInstance().logout(true, new EMCallBack() {

            @Override
            public void onSuccess() {
              /*  Log.d(TAG, "logout: onSuccess");
                reset();
                if (callback != null) {
                    callback.onSuccess();
                }*/

                System.out.println();

            }

            @Override
            public void onProgress(int progress, String status) {
               /* if (callback != null) {
                    callback.onProgress(progress, status);
                }*/
                System.out.println();
            }

            @Override
            public void onError(int code, String error) {
               /* Log.d(TAG, "logout: onSuccess");
                reset();
                if (callback != null) {
                    callback.onError(code, error);
                }*/

                System.out.println();
            }
        });
    }

    private void hxkfLoginOut() {
        //第一个参数为是否解绑推送的devicetoken
        ChatClient.getInstance().logout(true, new Callback(){
            @Override
            public void onSuccess() {
                System.out.println();
            }

            @Override
            public void onError(int code, String error) {
                System.out.println();
            }

            @Override
            public void onProgress(int progress, String status) {
                System.out.println();
            }
        });
    }


    private void cleardata() {
        SharedPreferences MSP = getSharedPreferences(Constant.MSP, Context.MODE_PRIVATE);
        MSP.edit().putString(Constant.ACCESS_TOKEN, "").apply();
        MSP.edit().putString(Constant.REFRESH_TOKEN, "").apply();
        MSP.edit().putString(Constant.ACCESS_TOKENTIME, "").apply();
    }

}